<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>留言板管理</title>
    <script src="js/vue.js" type="text/javascript" charset="utf-8"></script>
    <script src="js/axios-0.18.0.js"></script>
    <script src="js/jquery.min.js"></script>
    <!-- 引入样式 -->
    <link rel="stylesheet" href="https://unpkg.com/element-ui/lib/theme-chalk/index.css">
    <!-- 引入组件库 -->
    <script src="https://unpkg.com/element-ui/lib/index.js"></script>
</head>
<body>
<div id="app">
    <template class="table">
        <el-table
                :data="messageData"
                style="width: 100%">
            <el-table-column label="序号" type="index">
            </el-table-column>
            <el-table-column label="留言人" prop="fromPeople">
            </el-table-column>
            <el-table-column label="留言信息" prop="message">
            </el-table-column>
            <el-table-column align="center">
                <template slot="header" slot-scope="scope">
                    <el-input v-model="messageText" size="mini" placeholder="搜索留言" @blur="onChange" />
                </template>
                <template slot-scope="scope">
                    <el-button size="mini" type="danger" icon="el-icon-delete" @click="deleteMessage(scope.$index, scope.row)"></el-button>
                </template>
            </el-table-column>
        </el-table>
    </template>

    <!-- 分页 -->
    <el-pagination
            :current-page="page"
            :page-size="pageSize"
            :total="total"
            style="padding: 30px 0; text-align: center"
            layout="total, prev, pager, next, jumper"
            @current-change="getAllmessage"
    />
</div>
<div>

</div>
</body>
<script>
    new Vue({
        el:"#app",
        data:{
            //分页数据
            page:1,
            pageSize:10,
            total:0,
            messageData: [],//留言板的数据
            messageText:""//搜索框的内容
        },
        methods:{
            onChange(){//当搜索输入框失去焦点的方法
                //把输入表单的数据 再次请求后端 获取数据
                console.log(this.messageText)
                this.getAllmessage()
            },
            deleteMessage(index,row){//

                //给用户做提示 的确认按钮
                this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    console.log("删除文件")
                    //调用接口删除文件
                    axios.delete("/message?id="+row.id).then(resp=>{
                        if (resp.data.flag){
                            this.$message({
                                type:"success",
                                message:"删除成功!"
                            })
                            //再次重新获取数据
                            this.getAllmessage()
                        }else {
                            this.$message({
                                type:"error",
                                message:"删除请先登录后再进行删除"
                            })
                        }
                    })
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                });
            },
            getAllmessage(page = 1){//获取所有文件的方法
                this.page = page
                axios.get("/message/getPage?page="+this.page+"&"+"pageSize="+this.pageSize+"&messageText="+this.messageText).then(resp=>{
                    //给表单列表赋值
                    this.messageData=resp.data.data.page.records
                    //给总页码赋值
                    this.total=resp.data.data.page.total
                }).catch(error=>{
                    this.$message.error('网络出错,请刷新页面重试');
                })
            },
            imgUrl() {
                var num = Math.round(Math.random()*4);
                return './img/tou'+num+'.png'
            }
        },
        created(){
            this.getAllmessage()
        }
    })
</script>
</html>
